package com.fan.teamer.controller;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;
import org.springframework.security.web.WebAttributes;

import com.fan.teamer.service.MemberService;

@ManagedBean(name = "login")
@RequestScoped
public class LoginBean extends TeamerBean {

	private static Logger logger = Logger.getLogger("LoginBean");

	private String username;
	private String password;

	@ManagedProperty(value = "#{memberService}")
	private MemberService memberService;

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public String getUsername() {
		logger.debug("LoginBean: getUsername()");
		return username;
	}

	public void setUsername(String username) {
		logger.debug("LoginBean: setUsername()");
		this.username = username;
	}

	public String getPassword() {
		logger.debug("LoginBean: getPassword()");
		return password;
	}

	public void setPassword(String password) {
		logger.debug("LoginBean: setPassword()");
		this.password = password;
	}

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		LoginBean.logger = logger;
	}

	public String loginAction() throws Exception {
		logger.debug("LoginBean: loginAction()");

		ExternalContext context = FacesContext.getCurrentInstance()
				.getExternalContext();
		RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
				.getRequestDispatcher("/j_spring_security_check");
		dispatcher.forward((ServletRequest) context.getRequest(),
				(ServletResponse) context.getResponse());

		FacesContext.getCurrentInstance().responseComplete();

		logger.debug("LoginBean: username=" + username);
		logger.debug("LoginBean: password=" + password);

		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage();
		facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
		facesMessage.setDetail("hahaha");
		facesMessage.setSummary("aaaa");
		facesContext.addMessage(null, facesMessage);

		return null;
	}

	public String registerAction() {
		logger.debug("LoginBean: registerAction()");

		return "register";

	}

	public void updateMessages(boolean update) throws Exception {
		System.out.println("Start LoginBean.updateMessages");
		Exception ex = (Exception) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap()
				.get(WebAttributes.AUTHENTICATION_EXCEPTION);

		if (ex != null) {
			logger.error("Authentication Failed! ", ex);
			System.err.println("Authentication Failed! " + ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(
					"errorMessage",
					new FacesMessage(FacesMessage.SEVERITY_ERROR, ex
							.getMessage(), ex.getMessage()));
		}
		System.out.println("End LoginBean.updateMessages");
	}

}
